package com.woniuxy.service.impl;

import com.woniuxy.entity.User;
import com.woniuxy.mapper.UserMapper;
import com.woniuxy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 马宇航
 * @Todo: TODO
 * @DateTime: 25/08/26/星期二 17:33
 * @Component: 成都蜗牛学苑
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    //缓存的key 是方法名 users::findAll 成为key
    @Cacheable(value = "users",key = "#root.methodName")
    public List<User> findAll() {
        return  userMapper.findAll();
    }

    @Override
    //传入的实参 username 成为key的一部分
    @Cacheable(value = "userSearch",key = "#username" )
    public User findUserByUsername(String username) {
        return userMapper.findByUsernameLike("%"+username+"%");
    }

    @Override
    @CacheEvict(value = "users",allEntries = true) //清除目标key以users开头的缓存
    public int updateOne(User user) {
        User save = userMapper.save(user);
        if (save!=null){
            return 1;
        }else return 0;
    }
}
